/*
 * 本文件定义菜单内容
 * 以供外部使用
 **/

import { defineStore } from 'pinia';

// 定义菜单项的类型
interface MenuItem {
  name: string; // 菜单名称
  isDirectory: boolean; // 是否是目录
  parent?: string; // 所属目录（仅对菜单项有效）
}

export const useMenuStore = defineStore('menu', {
  state: () => ({
    menus: {
      '/front': { name: '前台门户', isDirectory: true },
      '/front/view': { name: '前台首页', isDirectory: false, parent: '/front' },
      '/front/course': { name: '课程信息', isDirectory: false, parent: '/front' },
      '/front/announcement': { name: '公告信息', isDirectory: false, parent: '/front' },

      '/course': { name: '课程管理', isDirectory: true },
      '/course/manage': { name: '课程管理', isDirectory: false, parent: '/course' },
      '/course/join-manage': { name: '课程加入管理', isDirectory: false, parent: '/course' },

      '/exam': { name: '试题考试', isDirectory: true },
      '/exam/question': { name: '试题管理', isDirectory: false, parent: '/exam' },
      '/exam/paper': { name: '试卷管理', isDirectory: false, parent: '/exam' },
      '/exam/grade': { name: '试卷评分', isDirectory: false, parent: '/exam' },

      '/resource': { name: '教学资源', isDirectory: true },
      '/resource/manage': { name: '资源管理', isDirectory: false, parent: '/resource' },

      '/message/': { name: '留言板', isDirectory: true },
      '/message/manage': { name: '留言管理', isDirectory: false, parent: '/message/' },

      '/check-in/': { name: '签到', isDirectory: true },
      '/check-in/manage': { name: '签到管理', isDirectory: false, parent: '/check-in/' },
    } as Record<string, MenuItem>,
  }),
  getters: {
    // 获取所有菜单项
    getMenuNames: (state) => {
      return state.menus;
    },

    // 获取单个菜单项名称
    getMenuName: (state) => (path: string) => {
      return state.menus[path]?.name || path;
    },

    // 获取所有目录
    getDirectories: (state) => {
      return Object.entries(state.menus)
        .filter(([, item]) => item.isDirectory)
        .reduce((acc, [path, item]) => {
          acc[path] = item;
          return acc;
        }, {} as Record<string, MenuItem>);
    },

    // 获取某目录下的菜单项
    getMenuItemsByDirectory: (state) => (directory: string) => {
      return Object.entries(state.menus)
        .filter(([, item]) => !item.isDirectory && item.parent === directory)
        .reduce((acc, [path, item]) => {
          acc[path] = item;
          return acc;
        }, {} as Record<string, MenuItem>);
    },
  },
});